package com.quick.config;

import com.quick.job.JobBeanTest;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;

/**
 * @ClassName JobConfig
 * @Description TODO
 * @Author wangxin332
 * @Date 2018/10/19 上午11:38
 * @Version 1.0
 **/
@Configuration
public class JobConfig {
	@Resource
	private JobBeanTest jobBeanTest;
	@Bean(name = "testJobFactoryBean")
	public MethodInvokingJobDetailFactoryBean testJobFactoryBean(){
		MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
		methodInvokingJobDetailFactoryBean.setTargetObject(jobBeanTest);
		methodInvokingJobDetailFactoryBean.setTargetMethod("handlerTwo");
		methodInvokingJobDetailFactoryBean.setConcurrent(Boolean.FALSE);
		return methodInvokingJobDetailFactoryBean;
	}

	@Bean(name = "testCronTriggerFactoryBean")
	public CronTriggerFactoryBean testCronTriggerFactoryBean(@Qualifier("testJobFactoryBean")JobDetail jobDetail ){
		CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
		cronTriggerFactoryBean.setJobDetail(jobDetail);
		//一分钟执行一次
		cronTriggerFactoryBean.setCronExpression("0 /1 * * * ? ");
		return cronTriggerFactoryBean;
	}

	@Bean(name = "testSchedulerFactoryBean")
	public SchedulerFactoryBean testSchedulerFactoryBean(@Qualifier("testCronTriggerFactoryBean") Trigger cronTriggerFactoryBean){
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
		//默认开启
		schedulerFactoryBean.setAutoStartup(Boolean.TRUE);
		schedulerFactoryBean.setTriggers(cronTriggerFactoryBean);
		return schedulerFactoryBean;
	}
}
